<?php
$date = $_SESSION['selectedBackDate'];
$userCentreName = $_SESSION["centreName"];
$con = mysqli_connect("127.0.0.1", "root", null, "eldercaredb");
// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to mysqli: " . mysqli_connect_error();
} else {
    $result = mysqli_query($con, "SELECT attendancerecord.NRIC, client.CustomerName, centre.CentreName, 
        attendancerecord.SignInTimeStamp, attendancerecord.SignOutTimeStamp, 
        attendancerecord.Status 
        FROM attendancerecord, client, centre 
        WHERE DATE(SignInTimeStamp)='$date' 
        AND attendancerecord.NRIC = client.NRIC 
        AND attendancerecord.CentreId = centre.CentreId 
        AND centre.CentreName = '$userCentreName'");

    echo "<table class='tablereport' id='tblAttendance' border='5'  valign='top'><tr >
<th name='Number'>
<font face='Arial, Helvetica, sans-serif'>Number</font>
</th>
<th name='NRIC'>
<font face='Arial, Helvetica, sans-serif'>NRIC</font>
</th>
<th name='Customer Name'>
<font face='Arial, Helvetica, sans-serif'>Customer Name</font>
</th>
<th name='CentreName'>
<font face='Arial, Helvetica, sans-serif'>Centre Name</font>
</th>
<th name='Date'>
<font face='Arial, Helvetica, sans-serif'>Date</font>
</th>
<th name='TimeIn' >
<font face='Arial, Helvetica, sans-serif'>Time In</font>
</th>
<th name='TimeOut'>
<font face='Arial, Helvetica, sans-serif'>Time Out</font>
</th>
<th name='Status'>
<font face='Arial, Helvetica, sans-serif'>Status</font>
</th>
</tr>";
    $count = 1;
    while ($row = mysqli_fetch_array($result)) {
        echo"<tr>
	<td >
	<font face='Arial, Helvetica, sans-serif'>" . $count . "</font>
	</td >
	<td >
	<font face='Arial, Helvetica, sans-serif'>" . $row['NRIC'] . "</font>
	</td >
	<td >
	<font face='Arial, Helvetica, sans-serif'>" . $row['CustomerName'] . "</font>
	</td >
	<td >
	<font face='Arial, Helvetica, sans-serif'>" . $row['CentreName'] . "</font>
	</td >";
        $signin = strtotime($row['SignInTimeStamp']);
        $signintime = date('j F, Y', $signin);
        echo "
	<td >
	<font face='Arial, Helvetica, sans-serif'>" . $signintime . "</font>
	</td>
	";
        $signin = strtotime($row['SignInTimeStamp']);
        $signintime = date('H:i:s', $signin);
        echo"<td >
	<font face='Arial, Helvetica, sans-serif'>" . $signintime . "</font>
	</td>";
        if ($row['SignOutTimeStamp'] == '0000-00-00 00:00:00') {
            echo "
		<td >
		<font face='Arial, Helvetica, sans-serif'></font>
		</td>
		<td>
		<font face='Arial, Helvetica, sans-serif'><form method='POST'><input class='signoutbutton' id='signout" . $count . "'type='submit' name='signout" . $count . "'value='Sign Out' size='10' maxlength='9' align='center' onClick='this.style.display = 'none';signoutattendance(this.id)'/></form></font>
		</td>
		";
        } else {
            $signout = strtotime($row['SignOutTimeStamp']);
            $signouttime = date('H:i:s', $signout);
            echo"
		<td >
		<font face='Arial, Helvetica, sans-serif'>" . $signouttime . "</font>
		</td>
		<td>
		<font face='Arial, Helvetica, sans-serif'>" . $row['Status'] . "</font>
		</td >
		";
        } echo"
		

	</tr>"
        ;
        $count++;
    }
    echo "</table>";
    $count--;
    echo "Total number of elderly attended: " . $count . ".";

    mysqli_close($con);
}
?>